package com.jg.visitor.config.exception;

import com.jg.visitor.common.bean.ResponseBean;
import com.jg.visitor.common.bean.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.multipart.MaxUploadSizeExceededException;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionConfig {

    @ExceptionHandler(JgException.class)
    public Object handlerServiceException(JgException e){
        log.error("handlerServiceException",e);
        return ResponseBean.error(e.getErrorCode());
    }

    @ExceptionHandler(BaseException.class)
    public Object handlerBaseException(BaseException e){
        log.error("handlerBaseException",e);
        return ResponseBean.error(e.getCode(), e.getMessage());
    }

    @ExceptionHandler(Throwable.class)
    public Object handlerThrowable(Throwable e){
        log.error("Throwable",e);
        return ResponseBean.error(ServiceCode.INTERNAL_SERVER_ERROR);
    }

    @ExceptionHandler(MaxUploadSizeExceededException.class)
    public Object handlerMaxUploadSizeExceededException(MaxUploadSizeExceededException e){
        log.error("MaxUploadSizeExceededException",e);
        return ResponseBean.error(ServiceCode.MAX_UPLOAD_SIZE);
    }
}
